home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_319 / cnewssrc / uupc.lzh / uupc / genv.c < prev    next >
C/C++ Source or Header  |  1990-01-16  |  9KB  |  401 lines

  1. /*
  2.  *    genv.c
  3.  *
  4.  *    Copying and use of this program are controlled by the terms of the
  5.  *    Free Software Foundations GNU Emacs General Public License.
  6.  *
  7.  *    Copyright (C) 1987 Jeff Lydiatt
  8.  *    Version        0.1        09 July 87
  9.  *
  10.  *    $Id: genv.c,v 1.3 90/01/16 10:25:30 crash Exp Locker: crash $
  11.  */
  12.  
  13. #ifndef lint
  14. static char RCSid[] = "$Id: genv.c,v 1.3 90/01/16 10:25:30 crash Exp Locker: crash $";
  15. #endif /* lint */
  16.  
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include "genv.h"
  20.  
  21. /* Environment variables are in file "PROFILE" */
  22. #define PROFILE        "Uupc:Config/profile"
  23.  
  24. #define SAME    0
  25. #define MAXLINE 255
  26.  
  27. /*
  28.  *    Hostname-type defines...
  29.  */
  30. #define DMAILBOX    "crash"
  31. #define    DNAME        "ckctpa"            /* For UUPC comms */
  32. #define    DDOMAIN        "ckctpa.uucp"
  33. #define    DNODENAME    "ckctpa"            /* For the mailer */
  34. #define    DMAILSERVICE "boake2"
  35.  
  36. /*
  37.  *    Directory and file locations...
  38.  */
  39. #define DHOME        "Uupc:Mail"
  40. #define    DMAILDIR    "Uupc:Mail"
  41. #define    DCONFDIR    "Uupc:Config"
  42. #define    DSPOOLDIR    "Uupc:Spool"
  43. #define DLOGDIR        "Uupc:Config"
  44. #define DPUBDIR        "Uupc:Spool"
  45. #define DTEMPDIR    "Uupc:Spool"        /* Should be T: ?? */
  46. #define DNEWSDIR    "News:"                /* See C-News docs */
  47.  
  48. /*
  49.  *    Miscellaneous
  50.  */
  51. #define    DDEVICE        "SER:"
  52. #define    DSPEED        "2400"
  53. #define DPAGESIZE    "24"
  54. #define DTIMEZONE    "+0500"
  55.  
  56.  
  57. #define    TFILENAME    "tmpfile"
  58. #define    FILENAME     "%s/%s"
  59.  
  60. struct environment {
  61.     char    name[16];
  62.     char    value[64];
  63. };
  64. typedef struct environment ENV;
  65.  
  66. static ENV profile[] =  {
  67.     MAILBOX,         "",
  68.     NAME,             "",
  69.     DOMAIN,         "",
  70.     MAILDIR,         "",
  71.     HOME,             "",
  72.     CONFDIR,         "",
  73.     SPOOLDIR,         "",
  74.     LOGDIR,         "",
  75.     PUBDIR,         "",
  76.     NEWSDIR2,         "",
  77.     TEMPDIR,         "",
  78.     MAILSERVICE,     "",
  79.     NODENAME,         "",
  80.     DEVICE,         "",
  81.     SPEED,             "",
  82.     PAGESIZE,        "",
  83.     TIMEZONE,        "",
  84.     "",             "",
  85. };
  86.  
  87.  
  88. char    *name = NULL;
  89. char    *nodename = NULL;
  90. char    *home = NULL;
  91. char    *domain = NULL;
  92. char    *mailserv = NULL;
  93. char    *mailbox = NULL;
  94. char    *maildir = NULL;
  95. char    *confdir = NULL;
  96. char    *spooldir = NULL;
  97. char    *logdir = NULL;
  98. char    *pubdir = NULL;
  99. char    *tempdir = NULL;
  100. char    *newsdir = NULL;
  101. char    *device = NULL;
  102. char    *speed = NULL;
  103. char    *timezone = NULL;
  104. char    *pagesize = NULL;
  105.  
  106. /*--------------------------------------------------------------*/
  107. /*    getone: get next character file f.  f already open            */
  108. /*--------------------------------------------------------------*/
  109.  
  110. static int getone(f)
  111. FILE *f;
  112. {
  113.     char    c;
  114.     static char line[256];
  115.     static int    pos = 0;
  116.     static int    len = 0;
  117.  
  118.     if ( ++pos > len || line[pos] == '\0' ) {
  119.         if ( fgets( line, 255, f ) == NULL )
  120.             return EOF;
  121.         pos = 0;
  122.         len = strlen( line );
  123.     }
  124.     return line[pos];
  125. }
  126.  
  127.  
  128. /*--------------------------------------------------------------*/
  129. /*    getsym: get next symbol from file f.  f already open        */
  130. /*--------------------------------------------------------------*/
  131.  
  132. #define ID        1001
  133. #define DELIM    1002
  134. #define STR        1003
  135. #define EOL        1004
  136. #define OTHER    1005
  137. #define UNKNOWN -1000
  138.  
  139. static int    getsym(f, sym)
  140. FILE *f;
  141. char *sym;
  142. {
  143.     /*
  144.      *    Simple non reentrant, non reuseable get next symbol from file f.
  145.      *
  146.      *    Valid symbols are:
  147.      *        Type        Symbol Returned            Comment
  148.      *
  149.      *        ID            <identifier>            any valid c identifier.
  150.      *        DELIM        '='                        an equal sign.
  151.      *        STR            a string                anything between "" or ''.
  152.      *        EOL            '\n'                    a newline.
  153.      *        EOF            EOF                        the end of file marker.
  154.      *        OTHER       a character                anything else.
  155.      *
  156.      *    Comments begin with # and are delimited by an end of line.
  157.      */
  158.     static int    lastchar = UNKNOWN; /* Unknown */
  159.     int    c, delim;
  160.  
  161.     /* strip leading white space */
  162.  
  163.     if ( lastchar != UNKNOWN )
  164.         c = lastchar;
  165.     else
  166.         c = getone( f );
  167.  
  168.     while (c == ' ' || c == '\t')
  169.         c = getone(f);
  170.     lastchar = UNKNOWN;
  171.  
  172.     /* Comments are '#' delimited by EOL character */
  173.  
  174.     if (c == '#')
  175.         while (c != '\n' && c != EOF)
  176.             c = getone(f);
  177.  
  178.     if (c == EOF) return EOF;
  179.  
  180.     if (c == '\n') {            /* End of Line? */
  181.         strcpy(sym, "\n");
  182.         return EOL;
  183.     }
  184.     if (c == '=') {                /* Delimiter '='? */
  185.         strcpy(sym, "=");
  186.         return DELIM;
  187.     }
  188.     if (c == '\"' || c == '\'') {    /* String ? */
  189.         delim = c;
  190.         while ((c = getone(f)) != delim && c != EOF && c != '\n')
  191.             *sym++ = c;
  192.         *sym = '\0';
  193.         c = getone(f);
  194.         return STR;
  195.     }
  196.     if (isalpha(c)) {            /* Identifier ? */
  197.         *sym++ = c;
  198.         while ((c = getone(f)) == '_' || isalnum(c))
  199.             *sym++ = c;
  200.         *sym = '\0';
  201.         lastchar = c;
  202.         return ID;
  203.     }
  204.     *sym++ = c;
  205.     *sym = '\0';
  206.     return OTHER;
  207. }
  208.  
  209.  
  210. /*--------------------------------------------------------------*/
  211. /*    setenv: insert an environment variable into my list            */
  212. /*--------------------------------------------------------------*/
  213.  
  214. static void setenv(var, value)
  215. register char *var;
  216. char *value;
  217. {
  218.     register ENV *p;
  219.  
  220.     for (p = profile; *(p->name) != '\0'; ++p)
  221.         if (strcmp(p->name, var) == SAME) {
  222. /*            strcpy(p->name, var);        /* [FJE] Why do this?? */
  223.             strcpy(p->value, value);
  224.             return;
  225.         }
  226. }
  227.  
  228.  
  229. /*--------------------------------------------------------------*/
  230. /*    getenv: get pointer to value of environment variable        */
  231. /*--------------------------------------------------------------*/
  232.  
  233. static char    *getenv(var)
  234. register char *var;
  235. {
  236.     register ENV *p;
  237.  
  238.     for (p = profile; *(p->name) != '\0'; ++p) {
  239.         if (strcmp(p->name, var) == SAME) {
  240.             if (*p->value != '\0')
  241.                 return p->value;
  242.             else
  243.                 break;
  244.         }
  245.     }
  246.     return NULL;
  247. }
  248.  
  249.  
  250. /*--------------------------------------------------------------*/
  251. /*    readenv: read environment from a file.                         */
  252. /*--------------------------------------------------------------*/
  253.  
  254. static void readenv()
  255. {
  256.     FILE *f;
  257.     int    symval;
  258.     char name[MAXLINE+1], value[MAXLINE+1];
  259.  
  260. #ifdef FJE
  261.     setmem(name, 0, sizeof(name));
  262.     if (f = fopen("ENV:profile", "r")) {
  263.         if (fread(name, sizeof(name)-1, 1, f) != 1) {
  264.             strcpy(name, PROFILE);
  265.             printf(stderr, "Can't read PROFILE variable: using default\n");
  266.         }
  267.         fclose(f);
  268.     } else
  269.         strcpy(name, PROFILE);
  270.     if ((f = fopen(name, "r")) == NULL) {
  271.         fprintf(stderr, "Can't open %s: using defaults\n", name);
  272.         return;
  273.     }
  274. #else
  275.     if ((f = fopen(PROFILE, "r")) == NULL) {
  276.         fprintf(stderr, "Can't open %s: using defaults\n", PROFILE);
  277.         exit( NULL );
  278.     }
  279. #endif /* !FJE */
  280.     /*
  281.      *    File is layed out as follows:
  282.      *
  283.      *    <environment variable> '=' <ID> | <STRING> # comment....
  284.      */
  285.  
  286.     while ((symval = getsym(f, name)) != EOF) {
  287.         /* Skip over any comment lines */
  288.  
  289.         while (symval == EOL)
  290.             symval = getsym(f, name);
  291.         if (symval == EOF)
  292.             break;
  293.  
  294.         if (symval != ID) {
  295.             fprintf(stderr, "Bad environment variable name %s\n", name);
  296. #ifdef FJE
  297.             continue;
  298. #else
  299.             exit( NULL );
  300. #endif /* !FJE */
  301.         }
  302.         if ((symval = getsym(f, value)) != DELIM) {
  303.             fprintf(stderr, "Missing `=' in environment file\n");
  304. #ifdef FJE
  305.             continue;
  306. #else
  307.             exit( NULL );
  308. #endif /* !FJE */
  309.         }
  310.         if ((symval = getsym(f, value)) != ID && symval != STR) {
  311.             fprintf(stderr,
  312.                 "missing value for `%s' in environment file\n", name);
  313. #ifdef FJE
  314.             continue;
  315. #else
  316.             exit( NULL );
  317. #endif /* !FJE */
  318.         }
  319.         setenv(name, value);
  320.     }
  321.     fclose(f);
  322. }
  323.  
  324.  
  325. /*--------------------------------------------------------------*/
  326. /*    exitenv: free that memory when done!                        */
  327. /*--------------------------------------------------------------*/
  328.  
  329. void exitenv()
  330. {
  331.     return;
  332. }
  333.  
  334.  
  335. static void genv(thename, envname, dflt)
  336. char    **thename;
  337. char    *envname;
  338. char    *dflt;
  339. {
  340.     if ((*thename = getenv(envname)) == NULL) {
  341.         fprintf(stderr, "genv: %s not found, using %s\n", envname, dflt);
  342.         *thename = dflt;
  343.     }
  344. }
  345.  
  346.  
  347. void loadenv()
  348. {
  349.     readenv();    /* read the profile from a file */
  350.  
  351.     /* get environment var's */
  352.     genv( &name,        NAME,            DNAME );
  353.     genv( &nodename,    NODENAME,        DNODENAME );
  354.     genv( &domain,        DOMAIN,            DDOMAIN );
  355.     genv( &mailserv,    MAILSERVICE,    DMAILSERVICE );
  356.     genv( &home,        HOME,            DHOME );
  357.     genv( &mailbox,        MAILBOX,        DMAILBOX );
  358.     genv( &maildir,        MAILDIR,        DMAILDIR );
  359.     genv( &confdir,        CONFDIR,        DCONFDIR );
  360.     genv( &spooldir,    SPOOLDIR,        DSPOOLDIR );
  361.     genv( &logdir,        LOGDIR,            DLOGDIR );
  362.     genv( &pubdir,        PUBDIR,            DPUBDIR );
  363.     genv( &tempdir,        TEMPDIR,        DTEMPDIR );
  364.     genv( &newsdir,        NEWSDIR2,        DNEWSDIR );
  365.     genv( &device,        DEVICE,            DDEVICE );
  366.     genv( &speed,        SPEED,            DSPEED );
  367.     genv( &pagesize,    PAGESIZE,        DPAGESIZE );
  368.     genv( &timezone,    TIMEZONE,        DTIMEZONE );
  369. }
  370.  
  371.  
  372. void mkfilename(filename, dirname, name)
  373. char    *filename, *name;
  374. register char    *dirname;
  375. {
  376. #ifdef AMIGA
  377.     /*
  378.      *    Make sure that a slash doesn't immediately follow
  379.      *    a colon, or that there aren't going to be two slashes
  380.      *    adjacent in the resulting filename...
  381.      */
  382.     if (dirname[ strlen(dirname)-1 ] == ':' ||
  383.         dirname[ strlen(dirname)-1 ] == '/')
  384.             sprintf(filename, "%s%s", dirname, name);
  385.     else
  386. #endif /* AMIGA */
  387.         sprintf(filename, FILENAME, dirname, name);
  388. }
  389.  
  390.  
  391. #ifdef TEST
  392. main()
  393. {
  394.     register ENV *p;
  395.  
  396.     loadenv();
  397.     for (p = profile; *(p->name); ++p)
  398.         fprintf(stderr, "name=`%s', value=`%s'\n", p->name, p->value);
  399. }
  400. #endif /* TEST */
  401.